package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.enums.MessageType;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.PreRecord;
import cn.fintecher.pangolin.entity.elastic.PreRecordModel;
import cn.fintecher.pangolin.service.dataimp.elastic.DataImportRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.PreRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.PreRecordModelElastic;
import cn.fintecher.pangolin.service.dataimp.service.BaseService;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 10:26 2019/10/12
 */

@Service("ConfirmPreRecordTask")
public class ConfirmPreRecordTask {
    private static final Logger log = LoggerFactory.getLogger(ConfirmPreRecordTask.class);

    @Autowired
    private PreRecordElastic preRecordElastic;

    @Autowired
    private DataImportRecordElastic dataImportRecordElastic;

    @Autowired
    private PreRecordModelElastic preRecordModelElastic;

    @Autowired
    private BaseService baseService;

    @Autowired
    private ConfirmPreFuture confirmPreFuture;

    /**
     * 委前催记导入确认
     */
    @Async
    @Transactional
    public void confirmPreRecord(List<PreRecordModel> models, UserModel userModel, DataImportRecord record) {
        //创建任务集合
        List<CompletableFuture<List<PreRecord>>> list = new ArrayList<>();
        //分页处理信息
        int pageSize = 2000;
        int pageNumber = (int) Math.ceil((double) models.size() / (double) pageSize);
        for (int i = 0; i < pageNumber; i++) {
            List<PreRecordModel> preRecordModels = models.stream().skip(i * pageSize).limit(pageSize).collect(Collectors.toList());
            CompletableFuture<List<PreRecord>> task = confirmPreFuture.processPreRecord(preRecordModels, userModel);
            list.add(task);
        }
        List<PreRecord> preRecords = new ArrayList<>();
        try {
            for (CompletableFuture<List<PreRecord>> future : list) {
                List<PreRecord> preRecordList = future.get();
                preRecords.addAll(preRecordList);
            }
            if (!preRecords.isEmpty()) {
                RepositoryUtil.saveData(preRecordElastic, preRecords);
            }
            RepositoryUtil.saveData(preRecordModelElastic, models);
            //更新导入结果状态
            record.setStatus(ImportDataExcelStatus.CONFIRMED);
            record.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(record);
        } catch (InterruptedException | ExecutionException e) {
            log.error(e.getMessage(), e);
            record.setStatus(ImportDataExcelStatus.UN_CONFIRMED);
            record.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(record);
        } finally {
            baseService.sendMsg(MessageType.IMPORT_FOLLOW_CONFIRMED, userModel);
        }
    }
}
